<template>
  <el-col :md="item.span ? item.span : 6" :xs="24">
    <el-form-item
      :label="item.label"
      :label-width="item.labelwidth ? item.labelwidth  + 'px' : 80 + 'px'">
      <el-select
        size="small"
        v-if="item.type==='select'"
        v-bind="$attrs"
        v-on="$listeners"
        :clearable="true"
        :filterable="true"
        :multiple="item.multiple?true:false"
        :collapse-tags="true"
        @focus="remoteMethod"
      >
        <el-option
          size="small"
          v-for="o in item.dicUrl ? ajaxOptions : item.dicData"
          :key="o.value"
          :label="o.label || o.name"
          :value="o.value"
          :disabled="o.disabled"
        >
        </el-option>
      </el-select>
      <el-date-picker
        size="small"
        v-else-if="item.type==='daterange'"
        type="daterange"
        range-separator="~"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        unlink-panels
        :picker-options="pickerOptions"
        :clearable="true"
        value-format="yyyy-MM-dd"
        v-bind="$attrs"
        v-on="$listeners">
      </el-date-picker>
      <el-input :placeholder="item.placeholder ? item.placeholder : '请输入'+item.label"
        v-else
        clearable
        size="small"
        :type="item.type"
        v-bind="$attrs"
        v-on="$listeners" />
    </el-form-item>
  </el-col>
</template>

<script>
import { api } from '@/api/common';
import { isNull } from '@/util/util'

export default {
  'props': {
      'item': Object
  },
  data() {
    return {
      'value': '',
      'ajaxOptions': [],
      pickerOptions: {
        shortcuts: [{
          text: '当天',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '半年',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 183)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '一年',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 365)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '三年',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 1095)
            picker.$emit('pick', [start, end])
          }
        }]
      }
    }
  },
  created() {
  },
  methods: {
    // 远程方式加载下拉款
    remoteMethod() {
      let _this = this
      // eslint-disable-next-line
      const { dicUrl, optionsCode, code } = _this.item;
      if (!isNull(dicUrl) && isNull(optionsCode)) {
        let params = {}
        let dicUrlArr = dicUrl.split('?')
        if (dicUrlArr.length > 1) {
          let paramUrl = dicUrlArr[1]
          let paramUrlArr = paramUrl.split('&')
          for(let i=0;i<paramUrlArr.length;i++){
            var paramArr = paramUrlArr[i].split('=')
            params[paramArr[0]] = paramArr[1]
          }
        }
        let url = dicUrlArr[0]
        if (api.hasOwnProperty(dicUrlArr[0])) {
          url = api[dicUrlArr[0]]
          _this.axios.post(url, params) //全局引入使用vue原型中的方法this.$http,已经把axios添加到原型中
          .then((res)=>{
            console.log(res)
            _this.ajaxOptions = res.data.data;
          })
          .catch((err)=>{
            console.log('调用失败',err)
          })
        } else {
          _this.axios.get(dicUrl, params) //全局引入使用vue原型中的方法this.$http,已经把axios添加到原型中
          .then((res)=>{
            console.log(res)
            _this.ajaxOptions = res.data.data;
          })
          .catch((err)=>{
            console.log('调用失败',err)
          })
        }        
      }
    }
  }
};
</script>